package cn.chousensei.chousenseiblog.service.impl;

import cn.chousensei.chousenseiblog.dao.UserMapper;
import cn.chousensei.chousenseiblog.enums.UserTypeEnum;
import cn.chousensei.chousenseiblog.model.User;
import cn.chousensei.chousenseiblog.service.UserService;
import cn.chousensei.chousenseiblog.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Boolean isSame(String account) {
        List<User> userList = userMapper.findAllUser();
        Set<String> accounts = userList.stream().map(user -> user.getAccount()).collect(Collectors.toSet());
        return accounts.contains(account);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrUpdate(User user) {
        User dbUser = userMapper.findByAccountOrAccountId(user);
        if (dbUser == null) {
            user.setGmtCreate(System.currentTimeMillis());
            user.setGmtModified(user.getGmtCreate());
            user.setToken(UUID.randomUUID().toString());
            user.setType(UserTypeEnum.VISITOR.getType());
            if (StringUtils.isBlank(user.getAvatarUrl())) {
                user.setAvatarUrl(Constant.DEFAULT_AVATAR);
            }
            if (!StringUtils.isBlank(user.getOpenAccountName()) && StringUtils.isBlank(dbUser.getUsername())) {
                user.setUsername(user.getOpenAccountName());
            }

            userMapper.insert(user);
        } else {
            dbUser.setGmtModified(System.currentTimeMillis());
            dbUser.setAvatarUrl(user.getAvatarUrl());
            dbUser.setOpenAccountId(user.getOpenAccountId());
            dbUser.setOpenAccountName(user.getOpenAccountName());
            dbUser.setToken(user.getToken());

            userMapper.updateSelective(user);
        }
    }

    @Override
    public User isLoginSuccess(User user) {
        User dbUser = userMapper.findByAccountOrAccountId(user);
        boolean isSuccess = dbUser.getPassword().equals(user.getPassword());
        if (isSuccess) {
            BeanUtils.copyProperties(dbUser, user);
            return user;
        } else {
            return null;
        }
    }

    @Override
    public User isLoggedIn(Cookie[] cookies) {
        if (cookies != null && cookies.length != 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    String token = cookie.getValue();
                    return userMapper.findByToken(token);
                }
            }
        }
        return null;
    }
}
